<?php
//模块名_控制名
class Warehouse_ProductsController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "warehouse/products/";	//控制对应模板目录
	}
	
	public function indexAction(){
		$warehouse = Warehouse_Service_Warehouse::getAll();
		$total = Warehouse_Service_Products::getJoinByCond(array(),'count(*)','wp_id',0,0);
		
		$this->view->warehouse = $warehouse;
		$this->view->total = $total;
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");	
    }
    
    public function listAction(){
    	$customer_name = $this->_request->getParam('customer_name','');
    	$warehouse_id_sch = $this->_request->getParam('warehouse_id_sch','');
    	$wa_code_sch = $this->_request->getParam('wa_code_sch','');
    	$wh_code_sch = $this->_request->getParam('wh_code_sch','');
    	
    	$cond =array();
    	if(!empty($customer_name)){
    		$customerRow = array();
    		$customerRow = Customer_Service_Customer::getByCustomerName($customer_name);
    		if($customerRow){
    			$cond['customer_id'] = $customerRow['customer_id'];
    		}    		
    	}
    	if($warehouse_id_sch){
    		$cond['warehouse_id'] = $warehouse_id_sch;
    	}
    	if($wa_code_sch){
    		$cond['wa_code'] = $wa_code_sch;
    	}
    	if($wh_code_sch){
    		$cond['wh_code'] = $wh_code_sch;
    	}

    	$page = $this->_request->getParam('page',1);
    	$pageSize = $this->_request->getParam('pageSize');
		$page = $page?$page:1;
    	$orderby = 'wp_id';
		$productsArr = Warehouse_Service_Products::getJoinByCond($cond,'*',$orderby, $pageSize, $page);
		
		$result = array();
		foreach ($productsArr as $key =>$val){
			$tmpArr = array();
			$tmpArr['wp_id'] = $val['wp_id'];
			$tmpArr['ws_code'] = $val['ws_code'];
			$tmpArr['wp_quantity'] = $val['wp_quantity'];
			$tmpArr['wa_code'] = $val['wa_code'];
			$tmpArr['wh_code'] = $val['wh_code'];
			$tmpArr['product_title'] = $val['product_title'];
			$tmpArr['product_sku'] = $val['product_sku'];
			// 产品ID $val['product_id']
			$customerRow = array();
			if($val['customer_id']){
				$customerRow = Customer_Service_Customer::getByCustomerId($val['customer_id']);
			}
			if( count($customerRow) ){
				$tmpArr['customer_name'] = $customerRow['customer_id'];				
			}
			$result[] = $tmpArr;
		}
		$total = count($result);
		$data=array('ask'=>0);
		if($total>0){
			$data=array(
				'total'=>$total,
				'result'=>$result,
				'ask'=>1
			);			
		}
		die(Zend_Json::encode($data));		
    }
    
    public function editAction(){
    	$wp_id = $this->_request->getParam('wp_id','');
    	$warehouse_id = $this->_request->getParam('warehouse_id','');
    	$ws_code = $this->_request->getParam('ws_code','');
    	$product_id = $this->_request->getParam('product_id','');
    	
    	$wp_quantity = $this->_request->getParam('wp_quantity','');
    	$wh_primary = $this->_request->getParam('wh_primary','');
    	
    	$row = array(
			'warehouse_id' => $warehouse_id,
			'ws_code' => $ws_code,
			'product_id' => $product_id,
			
			'wp_quantity' => $wp_quantity,
			'wh_primary' => $wh_primary,			
		);
		
		if($wp_id){
			$result = Warehouse_Service_Products::update($row,$wp_id);
		}else {
			$result = Warehouse_Service_Products::add($row);
		}
		
		if($result){
			$msg = array('ask'=>1);
    	}else{
    		$msg = array('ask'=>0);
    	}
		echo json_encode($msg);	
    }
    
    public function deleteAction(){
    	$id = $this->_request->getParam('id','');
    	if(!empty($id)){
    		$result = Warehouse_Service_Products::delete($id);
    		if($result){
    			$msg = array('ask'=>1);
    		}else{
    			$msg = array('ask'=>0);
    		}
    		echo json_encode($msg);
    	}

    }
    
    public function singleAction(){
    	$wp_id = $this->_request->getParam('id','');
    	if($wp_id){
    		$result = Warehouse_Service_Products::getByWpid($wp_id);
    		$data=array('ask'=>0);
    		if( count($result) ){
				$data=array(
					'result'=>$result,
					'ask'=>1
				);				
			}
			die(Zend_Json::encode($data));
    	}
    }
	/**
	*根据Product_id获取库存信息
	*/
    public function getinventoryAction(){
    	$p_id = $this->_request->getParam('product_id','');
    	if($p_id){
    		$result = Warehouse_Service_Products::getByProductId($p_id);
    		$data=array('ask'=>0);
    		if( count($result) ){
				$wrows=Warehouse_Service_Warehouse::getById($result['warehouse_id']);
				$result['warehouse_id']=$wrows['warehouse_name'];
				$data=array(
					'result'=>$result,
					'ask'=>1
				);
			}
			die(Zend_Json::encode($data));
    	}
    }
    
    public function getareaAction(){
    	$warehouse_id = $this->_request->getParam('id','');
    	$result = Warehouse_Service_Area::getByWarehouseId($warehouse_id);
    	$data=array('ask'=>0);
    	if(count($result)){
			$data=array(
				'result'=>$result,
				'ask'=>1
			);			
		}
		die(Zend_Json::encode($data));    	
    }
    
    public function gethouseAction(){
    	$wa_code = $this->_request->getParam('id','');
    	$result = Warehouse_Service_House::getByWaCode($wa_code);
    	$data=array('ask'=>0);
    	if(count($result)){
			$data=array(
				'result'=>$result,
				'ask'=>1
			);			
		}
		die(Zend_Json::encode($data));    	
    }
    
    
}